package com.b2c.oa.controller;

import com.b2c.common.DataRow;
import com.b2c.common.api.ApiResult;
import com.ims.entity.LiveDataReportEntity;
import com.ims.entity.MediaDataReportEntity;
import com.ims.entity.vo.StatisticsDataVo;
import com.ims.entity.vo.MediaListVo;
import com.ims.service.LiveDataReportService;
import com.ims.service.MediaAccountService;
import com.ims.service.MediaDataReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class AjaxController {
    @Autowired
    private MediaAccountService mediaAccountService;
    @Autowired
    private MediaDataReportService reportService;
    @Autowired
    private LiveDataReportService liveDataReportService;

    /**
     * 获取媒体账号列表
     * @return
     */
    @RequestMapping("/ajax/getMediaList")
    public ApiResult<List<MediaListVo>> mediaList(){
        var list = mediaAccountService.getMediaList(null);
        return new ApiResult<>(0,"",list);
    }

    /**
     * 添加新媒体运营数据
     * @param data
     * @return
     */
    @RequestMapping(value = "/ajax/media_data_input",method = RequestMethod.POST)
    public ApiResult<Integer> mediaDataInput(@RequestBody MediaDataReportEntity data, HttpServletRequest request){
        if(data.getAccountId()==null || data.getAccountId()==0)
            return new ApiResult<>(506,"参数错误，缺少AccountId");
        if(StringUtils.isEmpty(data.getPublishDate()))
            return new ApiResult<>(506,"参数错误，缺少PublishDate");
        if(data.getUserCount()==null || data.getUserCount()==0)
            return new ApiResult<>(506,"参数错误，请填写当前粉丝数");
        if(data.getViewCount()==null || data.getViewCount()==0)
            return new ApiResult<>(506,"参数错误，请填写观看量");
        if(data.getZanCount()==null || data.getZanCount()==0)
            return new ApiResult<>(506,"参数错误，请填写点赞数");
        if(data.getCommentCount()==null || data.getCommentCount()==0)
            return new ApiResult<>(506,"参数错误，请填写评论数");

        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");

        data.setReportUserId(Integer.parseInt(userId.toString()));

        //插入数据
        Integer reportId = reportService.addData(data);
        return new ApiResult<>(0,"",reportId);
    }

    /**
     * 添加新媒体运营数据
     * @param data
     * @return
     */
    @RequestMapping(value = "/ajax/live_data_input",method = RequestMethod.POST)
    public ApiResult<Integer> liveDataInput(@RequestBody LiveDataReportEntity data, HttpServletRequest request){
        if(data.getAccountId()==null || data.getAccountId()==0)
            return new ApiResult<>(506,"参数错误，缺少AccountId");
        if(StringUtils.isEmpty(data.getLiveDate()))
            return new ApiResult<>(506,"参数错误，请选择直播日期");
        if(StringUtils.isEmpty(data.getStartTime()))
            return new ApiResult<>(506,"参数错误，请选择开播时间");
        if(StringUtils.isEmpty(data.getEndTime()))
            return new ApiResult<>(506,"参数错误，请选择下播时间");
        if(data.getViewCount()==null || data.getViewCount()==0)
            return new ApiResult<>(506,"参数错误，请填写观看人数");
        if(data.getViewCount()==null )
            return new ApiResult<>(506,"参数错误，请填写观看量");
        if(data.getNewUser()==null )
            return new ApiResult<>(506,"参数错误，请填写新增粉丝");
        if(data.getCommentCount()==null )
            return new ApiResult<>(506,"参数错误，请填写评论数");
        if(data.getSource1()==null ||data.getSource2()==null ||data.getSource3()==null ||data.getSource4()==null ||data.getSource5()==null )
            return new ApiResult<>(506,"参数错误，请填写观看来源");
        if(data.getOrderCount()==null )
            return new ApiResult<>(506,"参数错误，请填写订单数");
        if(data.getOrderAmount()==null )
            return new ApiResult<>(506,"参数错误，请填写订单金额");
        HttpSession session = request.getSession();
        Object userId = request.getSession().getAttribute("userId");

        data.setCreateUserId(Integer.parseInt(userId.toString()));

        //插入数据
        Integer reportId = liveDataReportService.add(data);
        return new ApiResult<>(0,"",reportId);
    }
    /**
     * 获取媒体账号列表
     * @return
     */
    @RequestMapping("/ajax/getMediaData")
    public ApiResult<StatisticsDataVo> getMediaData(@RequestBody DataRow req){
        var list = liveDataReportService.getMediaData(req.getInt("accountId"));
        return new ApiResult<>(0,"",list);
    }

    /**
     * 获取媒体账号列表
     * @return
     */
    @RequestMapping("/ajax/getliveData")
    public ApiResult<StatisticsDataVo> getliveData(@RequestBody DataRow req){
        var list = liveDataReportService.getLiveData(req.getInt("accountId"));
        return new ApiResult<>(0,"",list);
    }

}
